

package com.scalar.chiptrack.operations.dataentry.actions;


import java.util.ArrayList;
import java.util.StringTokenizer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.db.DBTransaction;
import com.scalar.chiptrack.operations.dataentry.dao.BinQuantity;
import com.scalar.chiptrack.operations.dataentry.dao.SortDataEntryDAO;
import com.scalar.chiptrack.operations.dataentry.dao.TestDataEntryDAO;
import com.scalar.chiptrack.operations.dataentry.forms.BinDataEntryForm;
import com.scalar.chiptrack.operations.dataentry.forms.SortDataEntryForm;
import com.scalar.chiptrack.operations.process.Lot;
import com.scalar.chiptrack.operations.process.VendorLocation;
import com.scalar.chiptrack.operations.process.dao.LotDAO;
import com.scalar.chiptrack.operations.vendor.dao.VendorDAO;



public final class BinDataEntrySaveAction extends ChipTrackAction {

    public BinDataEntrySaveAction() {
    }

public ActionForward execute (ActionMapping mapping, 
                                  ActionForm form, 
                                  HttpServletRequest request, 
                                  HttpServletResponse response) throws Exception {


		HttpSession ses = request.getSession();

		SortDataEntryForm sortDataEntryForm = (SortDataEntryForm)ses.getAttribute("sortdataentryform");

		//System.out.println("lotNo in Bin data entry Save "+sortDataEntryForm.getLotNo());


		//ses.removeAttribute("sortdataentryform");
		
		ArrayList binList =(ArrayList)ses.getAttribute("binQuantityList");
		BinQuantity binQuantity = null;
    
		
			ArrayList vendorLocationList = new ArrayList();
			VendorLocation vendorLocation = null;


		ArrayList sortLocationList=VendorDAO.getSelectedLocations(Integer.parseInt(sortDataEntryForm.getProcessStepId()));
	
				 
				for(int i=0;i<sortLocationList.size();i++)
			{
				
				vendorLocation=(VendorLocation)sortLocationList.get(i);
				vendorLocation.setLocationID(vendorLocation.getLocationID());
				vendorLocation.setLocationName(vendorLocation.getLocationName());
				vendorLocationList.add(new org.apache.struts.util.LabelValueBean(vendorLocation.getLocationName(),new Integer(vendorLocation.getLocationID()).toString()));				
			}

			

					String lot 				= sortDataEntryForm.getLotNo();
					StringTokenizer st		= new StringTokenizer(lot,"$");
					String lotId			= st.nextToken();
					String lotNumber		= st.nextToken();
					
					Lot  sortLot = LotDAO.getSortLotData(lotId);

					sortDataEntryForm.setWaferQuantity(sortLot.getQuantityIn());
					sortDataEntryForm.setQuantityOut(sortLot.getQuantityOut());
					sortDataEntryForm.setTestDateIn(sortLot.getDateIn());
					sortDataEntryForm.setTestDateOut(sortLot.getDateOut());
					sortDataEntryForm.setDieWafer(sortLot.getDieWafer());

				
			ArrayList lotNumberList = LotDAO.getRunningLotNumbers(sortDataEntryForm.getProcessStepId(),sortDataEntryForm.getLocationId());
			Lot lotData = null;
			java.util.ArrayList list = new java.util.ArrayList();

			for(int i=0;i<lotNumberList.size();i++){
			lotData =(Lot)lotNumberList.get(i);
			list.add(new org.apache.struts.util.LabelValueBean(lotData.getLotNumber(),lotData.getLotId()+"$"+lotData.getLotNumber()+"$"+lotData.getStatus()))	;
			}
			list.add(new org.apache.struts.util.LabelValueBean(lotNumber,lot))	;
			request.setAttribute("locationSelectList",vendorLocationList);
			request.setAttribute("lotNumbers",list);	
			request.setAttribute("waferQuantity",sortDataEntryForm.getWaferQuantity());

			DBTransaction dbTransaction = null;
			boolean success = false;

			
			SortDataEntryDAO sortDataEntryDao = new SortDataEntryDAO();

						
			BinDataEntryForm binDataEntryForm = (BinDataEntryForm) form;
			String [] binQuantitys = binDataEntryForm.getBinQuantity();

			TestDataEntryDAO testDataEntryDao = new TestDataEntryDAO();
	
			ArrayList acceptBinList = testDataEntryDao.getBinNumber(Integer.parseInt(sortDataEntryForm.getProcessStepId()));
			BinQuantity acceptBinQuantity = null;
			String acceptBin = "";
			String acceptBinNumber = "";
			int pass=0;


			for(int i=0;i<acceptBinList.size();i++){
			acceptBinQuantity =(BinQuantity)acceptBinList.get(i);
			acceptBin = acceptBinQuantity.getBinNumber();
			acceptBinNumber=acceptBin.substring(3,acceptBin.length());
			pass=pass+Integer.parseInt(binQuantitys[Integer.parseInt(acceptBinNumber)-1]);
			}

			int total = Integer.parseInt(sortDataEntryForm.getDieWafer());
			float yield = (new Float(pass).floatValue()/ new Float(total).floatValue())*100;


		int waferNo = Integer.parseInt((String)request.getParameter("waferNo"));
		//System.out.println("waferNo "+waferNo);
		//System.out.println("pass "+pass);
		//System.out.println("yield "+yield);
		//System.out.println("total "+total);





		String [] waferYield			= sortDataEntryForm.getWaferYield();
		String [] existingPass			= sortDataEntryForm.getPass();
		String [] existingTotal			= sortDataEntryForm.getTotal();

		waferYield[waferNo-1]			= String.valueOf(yield);
		existingPass[waferNo-1]			= String.valueOf(pass);
		existingTotal[waferNo-1]		= String.valueOf(total);

		sortDataEntryForm.setWaferYield(waferYield);
		sortDataEntryForm.setPass(existingPass);
		sortDataEntryForm.setTotal(existingTotal);
		
		String [] waferPass       = sortDataEntryForm.getPass();
		int totalPass			  = 0;	
	
		for(int i=0;i<waferPass.length;i++){
		if(!waferPass[i].equals("")){
		totalPass = totalPass+Integer.parseInt(waferPass[i]);
		}
		}

		String strWaferIn			= sortDataEntryForm.getWaferQuantity();
		String strRma				= sortDataEntryForm.getRma();
		String strDiePerWafer		= sortDataEntryForm.getDieWafer();	

		if(strWaferIn==null || strWaferIn.equals("") )
		strWaferIn = "0";
		if(strRma==null || strRma.equals("") )
		strRma = "0";
		if(strDiePerWafer==null || strDiePerWafer.equals("") )
		strDiePerWafer = "0";




		float waferIn		= new Float(strWaferIn).floatValue();
		float rma			= new Float(strRma).floatValue();
		float diePerWafer	= new Float(strDiePerWafer).floatValue();
		float sortYield = (new Float(totalPass).floatValue()/(waferIn-rma)/diePerWafer)*100;
		//System.out.println("sort yield "+sortYield);

		sortDataEntryForm.setYield(String.valueOf(sortYield));

		request.setAttribute("sortdataentryform",sortDataEntryForm);
		String action = "binSetUp";
		request.setAttribute("action",action);
		//System.out.println("action "+action);


		
	
		try
				{
				dbTransaction = DBTransaction.getInstance();
				dbTransaction.begin();		
				

				}
				catch (Throwable t)
				{
					t.printStackTrace();
				}
	

	    sortDataEntryDao.updateYield(dbTransaction,Integer.parseInt(lotId),sortYield);
		
		for(int i=0;i<binList.size();i++){
			binQuantity =(BinQuantity)binList.get(i) ;
			int binNumber = i+1;
			int binQty = Integer.parseInt(binQuantity.getInfBinId());
		
			
	 SortDataEntryDAO.saveBinQuantity(dbTransaction,binQty,binQuantitys[i],binNumber,pass,total,yield);
	 success =true;

		}

		try{
		 if (success == true) {
                    dbTransaction.commit();
                }
                else {
                    dbTransaction.rollback();
                }

		}catch(Exception e){e.printStackTrace();}

		return (mapping.findForward("success")); 
	}
	
}